#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :ascii.py
# @Time      :2021/3/14 11:43
# @Author    :Letquit
"""
文件说明：

"""
from PIL import Image

codeLib = "@B%8&WM#*oahkbdpqwnZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+`<>i!lI;:,~"
count = len(codeLib)
# print(count)  # 64


def transfrom(image_file):
    codePic = ''
    imode = list(image_file.getbands())
    print(imode)
    print(imode[-1])
    for h in range(0, image_file.size[1]):
        for w in range(0, image_file.size[0]):
            if imode[-1] == 'A':
                r, g, b, a = image_file.getpixel((w, h))
            elif imode[-1] == 'B':
                r, g, b = image_file.getbands((w, h))
            gray = int(r * 0.299 + g * 0.587 + b * 0.114)
            codePic += codeLib[int(((count - 1) * gray) / 256)]
        codePic += '\r\n'
    return codePic


IMG = open('D:\Code\\photo\\dlam.png', 'rb')
image_file = Image.open(IMG)
image_file = image_file.resize((int(image_file.size[0] * 0.35), int(image_file.size[1] * 0.175)))
print('Info:', image_file.size[0], ' ', image_file[1], ' ', count)

tmp = open('D:\Code\\photo\\dlam.txt', 'w')
tmp.write(transfrom(image_file))
tmp.close()
if __name__ == "__main__":
    run_code = 0
